"""
    自动化操作浏览器---学习通
        登录学习通网址
"""

from selenium import webdriver
import time

if __name__ == '__main__':
    path = '../chromedriver.exe'
    url = 'http://i.chaoxing.com/base'
    # 打开浏览器
    browser = webdriver.Chrome(path)
    # 前往指定链接
    browser.get(url)
    # 输入账号和密码
    # 输入账号
    browser.find_element_by_css_selector('.ipt-tel').send_keys('15660917807')
    # 输入密码
    browser.find_element_by_css_selector('.ipt-pwd').send_keys('cai123cai456')
    # current_window = browser.current_window_handle  # 获取当前窗口handle name
    # 点击登录
    browser.find_element_by_css_selector('#loginBtn').click()
    time.sleep(2)
    # print(browser.window_handles)
    # 获得课程集合   //iframe[@id='frame_content']
    iframe = browser.find_element_by_css_selector('#frame_content')
    # 切换到iframe标签之中
    browser.switch_to.frame(iframe)
    course_list = browser.find_elements_by_css_selector('.course-name')
    # 点击指定课程
    course_list[4].click()
    # time.sleep(2)
    # 点击任务
    while True:
        browser.switch_to.window(browser.window_handles[-1])
        orange = browser.find_elements_by_css_selector('.orange')
        if len(orange) == 0:        # 如果没有任务，跳出
            break
        # 点击第一课
        orange[0].click()
        time.sleep(1)
        browser.switch_to.window(browser.window_handles[-1])
        # browser.execute_script("document.querySelector('video').play()")
        # 进入iframe
        iframe_two = browser.find_element_by_css_selector('#iframe')
        browser.switch_to.frame(iframe_two)
        time.sleep(1)
        iframe_two = browser.find_element_by_css_selector('.ans-attach-online')
        browser.switch_to.frame(iframe_two)
        time.sleep(1)
        browser.find_element_by_css_selector('.vjs-big-play-button').click()
        # 点击2倍速播放
        browser.execute_script("document.querySelector(  '.vjs-menu-item-text').click()")
        # 执行点击静音按钮的JavaScript代码
        browser.execute_script("document.querySelector('.vjs-mute-control').click()")
        # 判断视频是否播放完成
        # 获取到视频的播放时间
        time.sleep(2)
        video_time = browser.find_element_by_css_selector('.vjs-duration-display').text
        print('总的播放时间%s' % str(video_time))
        all_time_int = int(video_time.split(':')[0])
        sleep_time = int(all_time_int/2)
        print('睡眠时间%d' % sleep_time)
        time.sleep(sleep_time*60)
        while True:
            display_time = browser.find_element_by_css_selector('.vjs-current-time-display').text
            print('当前播放时间%s' % str(display_time))
            # 播放完成
            if video_time == display_time:
                break
            else:
                # 每隔30秒执行一次判断
                time.sleep(30)
        browser.back()
    # 控制台打印
    print('该课程观看完毕')

    # player = browser.find_element_by_css_selector('.vjs-playback-rate')
    #
    # player.click()
    # time.sleep(1)
    # player.click()
    # time.sleep(1)
    # player.click()
# https://mooc1-1.chaoxing.com/mycourse/studentstudy?
# chapterId=122250327
# &courseId=201451925
# &clazzid=43839222
# &enc=d431ff2c4d4e63c6d5dcbfc36dd53721